package com.xiang.wrapper;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class RepeatableReadRequestWrapper extends HttpServletRequestWrapper {

  //body 参数
  private final byte[] bytes;

  public RepeatableReadRequestWrapper(HttpServletRequest request, HttpServletResponse response) throws IOException {
    super(request);
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    bytes = request.getReader().lines().collect(Collectors.joining()).getBytes();
  }

  @Override
  public BufferedReader getReader() throws IOException {
    return new BufferedReader(new InputStreamReader(getInputStream()));
  }

  @Override
  public ServletInputStream getInputStream() throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    return new ServletInputStream() {
      @Override
      public boolean isFinished() {
        return false;
      }

      @Override
      public boolean isReady() {
        return false;
      }

      @Override
      public void setReadListener(ReadListener readListener) {

      }

      @Override
      public int read() throws IOException {
        return bais.read();
      }

      @Override
      public int available() throws IOException {
        return bytes.length;
      }
    };
  }

}
